﻿Public Class 同步窗体

    Private Const FO_MOVE = &H1                '移动
    Private Const FO_COPY = &H2                '复制
    Private Const FO_DELETE = &H3              '删除
    Private Const FO_RENAME = &H4              '改名
    Private Const FOF_NOCONFIRMATION = &H10    '没有提示
    Private Const FOF_SILENT = &H4
    Private Const FOF_NOERRORUI = &H400

    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByVal lpFileOp As SHFILEOPSTRUCt) As Long

    Private Structure SHFILEOPSTRUCt
        Public hwnd As Long         '句柄
        Public wFunc As Long        '操作命令
        Public pFrom As String      '源文件夹
        Public pTo As String        '目标文件夹
        Public fFlags As Integer    '
        Public fAnyOperationsAborted As Long     '不详
        Public hNameMappings As Long             '不详
        Public lpszProgressTitle As String   '   当取 FOF_SIMPLEPROGRESS 时，此参数才被使用
    End Structure

    Private Function PathOperate(ByVal FormPath As String, ByVal ToPath As String, ByVal sOrder As String) As Boolean

        Dim udtPath As SHFILEOPSTRUCt
        udtPath.hwnd = 0
        udtPath.wFunc = sOrder
        udtPath.pFrom = FormPath
        udtPath.pTo = ToPath
        udtPath.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI
        PathOperate = Not CBool(SHFileOperation(udtPath))

    End Function


    Private Sub btn开始同步_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始同步.Click

        Dim str1 As String = ""
        Dim str2 As String = ""

        If ShowMessageResult("此操作可能发生不可逆转的结果，是否继续？") <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If

        If Me.chk数据库.Checked Then
            If rad本机他机.Checked Then
                GetPath("SQLITEDATA", str1, str2)
            Else
                GetPath("SQLITEDATA", str2, str1)
            End If
        End If
        UTIL.CUtil.CopyFolder(Me.Handle, str1, str2, rad数据库.Checked)

        If chk图片.Checked Then
            GetPath("images", str1, str2)
        End If
        UTIL.CUtil.CopyFolder(Me.Handle, str1, str2, rad图片.Checked)

        If chk附件.Checked Then
            GetPath("upload", str1, str2)
        End If
        UTIL.CUtil.CopyFolder(Me.Handle, str1, str2, rad附件.Checked)

        If chk歌曲.Checked Then
            GetPath("Song", str1, str2)
        End If
        UTIL.CUtil.CopyFolder(Me.Handle, str1, str2, Me.rad歌曲.Checked)

        If chk歌词.Checked Then
            GetPath("Lyrics", str1, str2)
        End If
        UTIL.CUtil.CopyFolder(Me.Handle, str1, str2, rad歌词.Checked)

        ShowMessageBox("同步成功")
    End Sub

    Private Sub GetPath(ByVal strFlag As String, ByRef str1 As String, ByRef str2 As String)

        Dim strPath As String = IO.Path.Combine(Me.GetCurrentDirectory(), strFlag)

        Dim strOrgialPath As String = strPath
        Dim strCurrentPath As String = Me.GetCurrentDirectory()
        If (strCurrentPath.EndsWith("\")) Then
            strCurrentPath = strCurrentPath.Substring(0, strCurrentPath.Length - 1)
        End If

        Dim strCurrentPath1 As String = Me.GetAppSettingConfig("synchronizatePath")
        If (strCurrentPath1.EndsWith("\")) Then
            strCurrentPath1 = strCurrentPath1.Substring(0, strCurrentPath.Length - 1)
        End If

        str1 = strOrgialPath
        'str2 = strOrgialPath.Replace(strCurrentPath, strCurrentPath1)
        str2 = strCurrentPath1
    End Sub
End Class
